package jsonmap

func MapToJsonMap(from, to map[string]interface{}) {
	mapToJsonMap(from, to)
	return
}

func mapToJsonMap(from, to map[string]interface{}) {
	for key, val := range from {
		to[key] = copyAny(val, to[key])
	}
	return
}

func copyAny(fromVal, toVal interface{}) (r interface{}) {
	switch fv := fromVal.(type) {
	case map[string]interface{}:
		if toVal == nil {
			toVal = map[string]interface{}{}
		}
		switch tv := toVal.(type) {
		case map[string]interface{}:
			mapToJsonMap(fv, tv)
			r = tv
		default:
			return
		}
	case []interface{}: // 数组不允许复杂的结构, 所以数组直接覆盖
		return fv
	default:
		r = InterfaceToJSONValue(fv)
	}
	return
}
